package com.um.jdy.app.user.controller;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.alibaba.fastjson.JSONObject;
import com.um.jdy.app.user.entity.dto.*;
import com.um.jdy.app.user.entity.vo.LoginVO;
import com.um.jdy.app.user.manager.LoginManager;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

@Api(tags = "登陆")
@RestController
@RequestMapping("/api/v1/user")
public class LoginController {

    @Autowired
    LoginManager loginManager;

    @ApiOperation("检查是否绑定手机号")
    @PostMapping("check_phone_binded")
    public Response<Boolean> checkPhoneBinded(@RequestBody @Valid OpenIdDTO dto){
        return Response.success(loginManager.checkPhoneBinded(dto.getOpenId()));
    }

    @ApiOperation("支付宝小程序登陆")
    @PostMapping("alipay/signin")
    public Response<LoginVO> alipaySignin(@RequestBody AlipayLoginParam param){
        return Response.success(loginManager.alipaySignin(param));
    }

    @ApiOperation("支付宝小程序手机号解码")
    @PostMapping("alipay/decode_mobile")
    public Response alipayMobileDecode(@RequestBody @Valid AliMobileDecodeParam param){
        return Response.success(loginManager.alipayMobileDecode(param));
    }


    @ApiOperation("微信小程序登陆")
    @PostMapping("wx/signin")
    public Response<LoginVO> wxSignin(@RequestBody @Valid WxLoginParam param){
        return Response.success(loginManager.wxSignin(param));
    }

    @ApiOperation("获取微信小程序open_id")
    @GetMapping("wx/openid")
    public Response<WxMaJscode2SessionResult> getWxOpenId(@ApiParam("微信小程序js_code") @NotBlank String js_code){
        return Response.success(loginManager.getWxOpenId(js_code));
    }

    @ApiOperation("微信小程序手机号解码")
    @PostMapping("wx/decode_mobile")
    public Response<WxMaPhoneNumberInfo> wxMobileDecode(@RequestBody @Valid WxMobileDecodeParam param){
        return Response.success(loginManager.wxMobileDecode(param));
    }

    @ApiOperation("绑定手机号")
    @PutMapping("bind_mobile")
    public Response bindMobile(@RequestBody @Valid BindMobileParam param){
        loginManager.bindMobile(param);
        return Response.success();
    }

}
